package android.medicalcloud.com.yiapp.yilian.ui.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.cach.PreferceManager;
import android.medicalcloud.com.yiapp.base.utils.DateTools;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.StringTools;
import android.medicalcloud.com.yiapp.yilian.service.DownLoadService;
import android.medicalcloud.com.yiapp.yilian.utils.android.Common;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;




/**
 *
 */
public class VersionDialog extends Dialog {
    Context context;


    public VersionDialog(final Context context) {
        super(context, R.style.CommonDialog);
        this.context = context;
        getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);
        setContentView(R.layout.dialog_version);
        findViewById(R.id.btUpdate).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringTools.isNullOrEmpty(dowLoadUrl)) {
                    Intent intent = new Intent(context, DownLoadService.class);
                    intent.putExtra("url", dowLoadUrl);
                    context.startService(intent);
                }
                dismiss();
            }
        });
        findViewById(R.id.btLater).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveShowFlag();
                dismiss();
            }
        });
        setCanceledOnTouchOutside(false);

    }

    @Override
    public void show() {
        Logger.e("isParentAlive =4343 "+!isParentAlive());

        if (!isParentAlive()) {
            return;
        }
        Logger.e("isSameDay =4343 "+isSameDay());
        if (isSameDay()) {
            return;
        }
        Logger.e("dowLoadUrl =4343 "+dowLoadUrl);
        if (StringTools.isNullOrEmpty(dowLoadUrl)) {
            return;
        }
        saveShowFlag();
        super.show();
    }


    String dowLoadUrl = "";

    public void setRes(String downLoadUrl, String updateMes,boolean isM) {
        TextView content = (TextView) findViewById(R.id.content);
         findViewById(R.id.btLater).setVisibility(isM?View.GONE:View.VISIBLE);
        content.setText(updateMes);
        this.dowLoadUrl = downLoadUrl;
    }


    @Override
    public void dismiss() {
        if (!isParentAlive()) {
            return;
        }
        super.dismiss();
    }

    private boolean isParentAlive() {
        try {
            Activity ac = (Activity) context;
            if (ac.isFinishing()) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    private String getDate() {
        String date = DateTools.dateFormatterOfDateTimeForNow(DateTools.DATE_FORMAT_STYLE_2);
        return date;
    }

    private void saveShowFlag() {
        PreferceManager.getInsance().saveValueBYkey("versionShow", getDate());
    }

    public boolean isSameDay() {
        Logger.e("isSameDay 11 "+PreferceManager.getInsance().getValueBYkey("versionShow"));
        Logger.e("isSameDay 12 "+getDate());
        return PreferceManager.getInsance().getValueBYkey("versionShow").equals(getDate());
    }


}
